package com.weetop.cfw.mine.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.ToastUtils
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.ConfirmationOfOrderPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.ConfirmationOfOrderView
import com.weetop.cfw.bean.AudioVideoDetailBean
import com.weetop.cfw.bean.OrderInfoBean
import com.weetop.cfw.constants.UrlConstants
import com.weetop.cfw.utils.PayUtils
import com.xgr.easypay.callback.IPayCallback
import kotlinx.android.synthetic.main.activity_confirmation_of_order.*

/**
 * 确认订单界面
 */
class ConfirmationOfOrderActivity : CommonBaseActivity(),View.OnClickListener,ConfirmationOfOrderView {

    private lateinit var confirmationOfOrderPresenterImp: ConfirmationOfOrderPresenterImp

    private var orderInfo:String = ""

    override fun getLayoutId() = R.layout.activity_confirmation_of_order

    override fun initView(savedInstanceState: Bundle?) {
        setViewsOnClickListener(this,imageConfirmationOfOrderBack,relativeConfirmPaymentContainer)

        confirmationOfOrderPresenterImp = ConfirmationOfOrderPresenterImp()
        confirmationOfOrderPresenterImp.attachView(this)


    }

    companion object{
        private const val ORDER_ID = "ORDER_ID"
        private const val INFOR_ID = "INFOR_ID"
        fun startConfirmationOfOrderActivity(context:Context,orderId:String,inforId:Int){
            val intent = Intent(context,ConfirmationOfOrderActivity::class.java)
            intent.putExtra(ORDER_ID,orderId)
            intent.putExtra(INFOR_ID,inforId)
            ActivityUtils.startActivity(intent)
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        val orderId = intent.getStringExtra(ORDER_ID)
        val inforId = intent.getIntExtra(INFOR_ID,0)
        if(orderId != null){
            confirmationOfOrderPresenterImp.getOrderInfo(this,orderId)

        }
        confirmationOfOrderPresenterImp.getCourseDetail(this,inforId)
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.imageConfirmationOfOrderBack -> ActivityUtils.finishActivity(this)
            R.id.relativeConfirmPaymentContainer -> PayUtils.aliPay(this,orderInfo,object:IPayCallback{
                override fun failed(code: Int, message: String?) {
                    ToastUtils.showShort("支付失败")
                }

                override fun cancel() {
                   ToastUtils.showShort("支付取消")
                }

                override fun success() {
                    ToastUtils.showShort("支付成功")
                    SuccessfulPaymentActivity.startSuccessfulPaymentActivity(this@ConfirmationOfOrderActivity,intent.getIntExtra(INFOR_ID,0),intent.getStringExtra(ORDER_ID))
                }

            })
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        confirmationOfOrderPresenterImp.detachView()
    }

    /**
     * 订单信息获取成功
     */
    override fun orderInfoGetSuccess(orderInfoBean: OrderInfoBean) {
//        textOrderNo.text = "订单号：${orderInfoBean.orderid}"
        orderInfo = orderInfoBean.sign
    }

    /**
     * 商业云课详情获取成功
     */
    override fun courseDetailGetSuccess(audioVideoDetailBean: AudioVideoDetailBean) {
        imageCourseLogo.setUrlImage("${UrlConstants.baseUrl}${audioVideoDetailBean.imgurl}")
        textCourseTitle.text = audioVideoDetailBean.title
        textCourseTeacherName.text = audioVideoDetailBean.author
        textCoursePlayNumber.text = audioVideoDetailBean.playnum
        SpanUtils.with(textCouponPrice).apply {
            append("￥")
            setFontSize(12,true)
            setForegroundColor(ColorUtils.string2Int("#F42626"))
            append(audioVideoDetailBean.sprice)
            setFontSize(18,true)
            setBold()
            setForegroundColor(ColorUtils.string2Int("#F42626"))
        }.create()
        SpanUtils.with(textOriginPrice).apply {
            append("￥")
            append(audioVideoDetailBean.mprice)
            setFontSize(12,true)
            setForegroundColor(ColorUtils.string2Int("#999999"))
            setStrikethrough()
        }.create()
    }
}
